<template>
	<view class="page">
		<view class="main">
			<view class="main-title">输入设备IMEI完成绑定</view>
			<view class="inputImei">
				<u--input placeholder="请输入内容" border="surround" inputAlign="center" shape="circle"
					v-model="imei"></u--input>
			</view>
			<view class="btn-box bgprimary" @click="bindImei">
				确定
			</view>
			<view class="btn-box bgdefault" @click="scanCode">
				扫码绑定(推荐)
			</view>
		</view>
		<PrivacyPop :showPrivacy="showPrivacy" @close="close" />
	</view>
</template>

<script>
	import PrivacyPop from '@/uni_modules/privacy.vue'
	import {
		mapState,
	} from 'vuex'
	export default {
		components: {
			PrivacyPop
		},
		computed: {
			...mapState(['member']),
		},
		data() {
			return {
				loglist:["log"],
				showPrivacy: false,//
				imei: '',
			};
		},
		onLoad() {
			// #ifdef MP-WEIXIN
				wx.onNeedPrivacyAuthorization((resolve, eventInfo) => {
					this.showPrivacy = true;
					this.needPrivacy = true;
				})
				wx.requirePrivacyAuthorize({
					success: (res) => {},
					fail: (err) => {}
				})
			// #endif
		},
		methods: {
			close() {
			    this.showPrivacy = false
			},
			bindImei() {
				uni.$u.http.post('/client/bind_imei', {
					imei: this.imei,
					phone: this.member.phone
				}).then(res => {
					if (res.status == 400) {
						return this.$u.toast(res.msg)
					}
					if (res.status == 200) {
						this.$u.toast('绑定成功！')
						setTimeout(() => {
							uni.reLaunch({
								url: "/pages/index/mime?update=1"
							}, 2000)
						})
					} else {
						return this.$u.toast('网络错误，绑定失败！')
					}

				})
			},
			scanCode() {
				if(this.needPrivacy){
					return this.showPrivacy = true;
				}
				
				uni.scanCode({
					success: (res) => {
						if(res.result.length>30 && res.result.search('dev_info') != -1){
							this.imei = res.result.slice(-18,-3)
							let istart = res.result.indexOf('"imei":"');
							if(istart>0 && res.result.length >= (istart+23)){
								this.imei = res.result.slice(istart+8,istart+23)
								return true;
							}
						}
						this.imei = res.result;
					},
					fail: (err) => {
						// console.log(err);
						// return this.$u.toast(err)
					}
				})
			},
			handleAgreePrivacyAuthorization() {
				this.scanCode();
			}
		},
	}
</script>

<style lang="scss" scoped>
	.main-title {
		height: 40rpx;
		line-height: 40rpx;
		font-size: 40rpx;
		font-weight: 500;
		color: #333;
		margin-bottom: 40rpx;
		text-align: center;
	}

	.btn-box {
		margin-top: 20rpx;
		height: 80rpx;
		line-height: 80rpx;
		text-align: center;
		border-radius: 40rpx;
	}

	.main {
		padding: 30rpx;
		margin: 26px auto;
		width: 312px;
		background: #fff;
		border-radius: 20rpx;
	}

	.inputImei {
		text-align: center;
		height: 80rpx;
		border-radius: 40rpx;
		border: none;
	}

	.stpes {
		margin-top: 40rpx;
	}

	.stepImg {
		width: 180rpx;
		height: 180rpx;
		border-radius: 20rpx;
		margin-right: 20rpx;
	}

	.item-title {
		height: 20px;
		line-height: 20px;
		font-size: 16px;
		color: #01d6d6;
		font-weight: 500;
		margin-bottom: 10px;
	}

	.item.text {
		line-height: 20px;
		font-size: 14px;
	}
</style>